{% extends 'base.html' %}

{% load static %}

{% block head_title %}Competitions (My CodaLab){% endblock head_title %}
{% block page_title %}Competitions (My CodaLab){% endblock page_title %}

{% block content %}

    <ul class="nav nav-tabs" id="my_competition_tab_nav">
        <li class="active">
            <a href="#competitions" role="tab" data-toggle="tab">Competitions</a>
        </li>
        <li>
            <a href="#my_participating" role="tab" data-toggle="tab">Competitions I'm In</a>
        </li>
        <li>
            <a href="#my_managing" role="tab" data-toggle="tab">Competitions I'm Running</a>
        </li>
        <li>
            <a href="{% url 'my_datasets' %}">My Datasets</a>
        </li>
    </ul>

    <div class="tab-content">
        <div id="competitions" class="tab-pane active">
            <div class="tab-inner">
                <div class="row">
                    <div class="col-sm-12 kill-newlines">
                        {% if not published_competitions %}
                            <p><em>There are no competitions.</em></p>
                        {% else %}
                            {% for competition in published_competitions %}
                                {% include "web/my/_competition_tile.html" with competition=competition %}
                            {% endfor %}
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <div id="my_participating" class="tab-pane">
            <div class="tab-inner">
                <div class="row">
                    <div class="col-sm-12 kill-newlines">
                        {% include "web/my/_entered.html" with participations=competitions_im_in %}
                    </div>
                </div>
            </div>
        </div>

        <div id="my_managing" class="tab-pane">
            <div class="tab-inner">
                <div class="row">
                    <div class="col-sm-12 kill-newlines">
                        {% if USE_AWS %}
                            <a class="btn btn-primary" target="_self" href="{% url 'competitions:s3_create_competition' %}">Create Competition</a>
                        {% else %}
                            <a class="btn btn-primary" target="_self" href="{% url 'competitions:create' %}">Create Competition</a>
                        {% endif %}
                            <a class="btn btn-primary" href="{% url 'queues:list' %}">Worker Queue Management</a>
                        <br><br>

                        {% for competition in my_competitions %}
                            {% include "web/my/_competition_tile.html" with competition=competition managed=True %}
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock content %}
{% block jsincludes %}
<script>
    // Disable scrolling to tab
    if (location.hash) {
        setTimeout(function() {
            window.scrollTo(0, 0);
        }, 1);
    }

    // Javascript to enable link to tab
    var url = document.location.toString();
    if (url.match('#')) {
        $(`.nav-tabs a[href="*#${url.split('#')[1]}"]`).tab('show')
    }

    // Change hash for page-reload
    $('.nav-tabs a').on('shown', function (e) {
        window.location.hash = e.target.hash;
    });


    $('.competition-publish-button').on('click', function (event) {
        event.preventDefault();
        var button = $(this);
        if (!confirm("Are you sure you wish to publish this competition?")) {
            return false;
        }
        $.get($(this).prop('href'))
            .done(function () {
                button.siblings('.competition-unpublish-button').removeClass('hide');
                button.addClass('hide');
            })
            .fail(function () {
                alert('Error publishing competition, check your Internet connection. If problem persists contact an administrator.');
            });


        return false;
    });
    $('.competition-unpublish-button').on('click', function(event) {
        event.preventDefault();
        var button = $(this);
        $.get($(this).prop('href'))
            .done(function() {
                button.siblings('.competition-publish-button').removeClass('hide');
                button.addClass('hide');
            })
            .fail(function() {
                alert('Error unpublishing competition, check your Internet connection. If problem persists contact an administrator.');
            });
        return false;
    });
</script>
{% endblock %}
